package hedron.dialog;

import static hedron.i18n.Internationalization._;
import hedron.Application;
import hedron.db.model.StoreName;
import hedron.db.model.StoredStock;
import hedron.ui.UIFactory;
import java.util.ArrayList;
import java.util.Map;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;

public class MoveSplitStockDialog extends AbstractDialog {
	final ArrayList<StoreName> storeNames;
	final StoreName currentStore;
	final StoredStock selectedStock;

	Combo storeCombo;
	Spinner qtySpinner;

	public MoveSplitStockDialog(Shell parentShell, StoredStock stock, StoreName currentStore) {
		super(parentShell);
		this.currentStore = currentStore;
		storeNames = Application.STORES.getStoreNames();
		selectedStock = stock;
	}

	@Override
	public void create() {
		super.create();
		setTitle(_("Dialog.MoveSplitStock.Title"));
		setMessage(_("Dialog.MoveSplitStock.Message") + " [ " + selectedStock.QTY + " ]", 0);
	}

	@Override
	protected void okPressed() {
		final long moveQty = (long) qtySpinner.getSelection();
		final long storeID = storeNames.get(storeCombo.getSelectionIndex()).getID();
		try {
			Application.STORES.moveSplitStock(selectedStock, storeID, moveQty);
			close();
		}catch (Exception e) {
			Application.setStatusMessage(2, e.getMessage());
		}
	}

	private static final String layout = "<layout columns=\"2\" width=\"400\" align=\"FILL\" valign=\"FILL\">"
			+ "<label text=\"Column.Stock\" width=\"100\" align=\"BEGINING\" valign=\"CENTER\"/>"
			+ "<label id=\"stockNameLabel\" align=\"FILL\" valign=\"CENTER\"/>"
			+ "<label text=\"Column.Store\" align=\"BEGINING\" valign=\"CENTER\"/>"
			+ "<combo id=\"storeCombo\" readonly=\"true\" align=\"FILL\" valign=\"CENTER\"/>"
			+ "<label text=\"Column.Quantity\" align=\"BEGINING\" valign=\"CENTER\"/>"
			+ "<spinner id=\"qtySpinner\" width=\"100\" height=\"16\" align=\"BEGINING\" valign=\"CENTER\"/>"
			+ "</layout>";

	@Override
	void createContent(Composite parent) {
		Map<String, Control> components = UIFactory.createUI(parent, layout, null);
		Label stockNameLabel = (Label) components.get("stockNameLabel");
		storeCombo = (Combo) components.get("storeCombo");
		qtySpinner = (Spinner) components.get("qtySpinner");

		stockNameLabel.setText(selectedStock.GROUP_NAME + " - " + selectedStock.STOCK_NAME);

		String[] storesNames = new String[storeNames.size()];
		int i = 0;
		int selection = 0;
		for (StoreName storeName : storeNames) {
			if (storeName == currentStore) selection = i;
			storesNames[i++] = storeName.STORE_NAME;
		}
		storeCombo.setItems(storesNames);
		storeCombo.select(selection);

		qtySpinner.setValues((int) selectedStock.QTY, 1, (int) selectedStock.QTY, 0, 1, 1);
	}

	private static final String layoutFooter = "<layout columns=\"3\" align=\"FILL\" valign=\"END\">"
			+ "<label align=\"FILL\" valign=\"CENTER\"/>"
			+ "<button id=\"cancel\" width=\"80\" height=\"20\" align=\"BEGINING\" valign=\"END\"/>"
			+ "<button id=\"ok\" default=\"true\" width=\"80\" height=\"20\" align=\"BEGINING\" valign=\"END\"/>"
			+ "</layout>";

	@Override
	void createFooter(Composite parent) {
		Composite grid = new Composite(parent, 0);
		Map<String, Control> components = UIFactory.createUI(grid, layoutFooter, null);

		Button cancel = (Button) components.get("cancel");
		cancel.setText("Cancel");
		cancel.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				setReturnCode(CANCEL);
				close();
			}
		});

		Button ok = (Button) components.get("ok");
		ok.setText("OK");
		ok.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				setReturnCode(OK);
				okPressed();
			}
		});
	}
}
